package LeetCode.exercise;

import java.util.LinkedList;
import java.util.Queue;

public class LC1765 {
    public static void main(String[] args) {
        LC1765 main = new LC1765();
        int[][] isWater = new int[][]{{0,0,1},{1,0,0},{0,0,0}};
        int[][] ints = main.highestPeak(isWater);
        System.out.println();
    }
    public int[][] highestPeak(int[][] isWater) {
        Queue<int[]> q = new LinkedList<>();
        int m = isWater.length, n = isWater[0].length, count = 0;
        boolean[][] flag = new boolean[m][n];
        int[][] res = new int[m][n];
        int[] dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(isWater[i][j] == 1){
                    q.offer(new int[]{i, j});
                    flag[i][j] = true;
                }
            }
        }
        while(!q.isEmpty()){
            count++;
            int size = q.size();
            while(size-- > 0){
                int[] tmp = q.poll();
                int a = tmp[0], b = tmp[1];
                for(int i = 0; i < 4; i++){
                    int x = a + dx[i], y = b + dy[i];
                    if(x >= 0 && x < m && y >= 0 && y < n && !flag[x][y]){
                        res[x][y] = count;
                        flag[x][y] = true;
                        q.offer(new int[]{x, y});
                    }
                }
            }
        }
        return res;
    }
}
